package com.hxalex.simulationhandle.ui.base.presenter;

import android.app.Activity;
import android.content.Context;
import android.os.RemoteException;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.common.BasePresenter;
import com.hxalex.simulationhandle.common.ResultModel;
import com.hxalex.simulationhandle.ui.base.model.JumpProtocol;
import com.hxalex.simulationhandle.widget.StarTDialog;
import com.thinkdit.lib.util.StringUtil;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

/**
 * 剪贴板检查的P类
 * Created by qiuda on 16/7/13.
 */
public class ClipboardCheckPresenter extends BasePresenter {
    private final String URL_JUMP_PROTOCOL = "/content/get-jump-protocol";
    private StarTDialog mNotifyDialog;
    private Context mContext;

    private JumpProtocol mJumpProtocol;

    public ClipboardCheckPresenter(Context context) {
        mContext = context;
    }

    public void checkText(ViewGroup viewGroup) {
        if (!(mContext instanceof Activity)) {
            return;
        }
        String activityName = ((Activity) mContext).getComponentName().getClassName();
        if (activityName.contains("StartActivity") || activityName.contains("ShareActivity")||activityName.contains("FlashScreenADActivity")) {
            return;
        }
        if (activityName.contains("LiveActivity")) {
            StringUtil.copyToClipboard(mContext, "");
            return;
        }
        String text = StringUtil.getClipboardText(mContext);
        if (!StringUtil.isNullorEmpty(text)) {
            String code = getCode(text);
            if (!StringUtil.isNullorEmpty(code)) {
                if (mNotifyDialog == null) {
                    initDialog(viewGroup);
                }
                getTextData(code);
            }
        }
    }

    private void initDialog(ViewGroup viewGroup) {
        mNotifyDialog = new StarTDialog(mContext, viewGroup);
        mNotifyDialog.setTextGravityCenter(true);
        mNotifyDialog.setCanceledOnTouchOutside(false);
        mNotifyDialog.setCancelable(false);
       
    }

    private String getCode(String text) {
        String key = "\uD83D\uDD12";
        String key1 = "\uE144";
        if (text.contains(mContext.getString(R.string.app_name)) && text.contains(mContext.getString(R.string.clipboard_check_text))) {
            if (text.contains(key)) {
                int indexStart = text.indexOf(key);
                int indexEnd = text.lastIndexOf(key);
                if (indexStart != indexEnd && indexStart + 2 != indexEnd) {
                    StringUtil.copyToClipboard(mContext, "");
                    return text.substring(indexStart + 2, indexEnd);
                }
            } else if (text.contains(key1)) {
                int indexStart = text.indexOf(key1);
                int indexEnd = text.lastIndexOf(key1);
                if (indexStart != indexEnd && indexStart + 1 != indexEnd) {
                    StringUtil.copyToClipboard(mContext, "");
                    return text.substring(indexStart + 1, indexEnd);
                }
            }
        }
        return null;
    }


    /**
     * 获取code对应的数据
     *
     * @param code code
     */
    private void getTextData(String code) {
        Map<String, Object> params = new HashMap<>();
//        params.put("userId", UserInfoManager.getManager(mContext).getUserId());
        params.put("code", code);
        get(getUrl(URL_JUMP_PROTOCOL), params, mContext);
    }

    @Override
    public Object asyncExecute(String url, ResultModel resultModel) {
        if (url.contains(URL_JUMP_PROTOCOL)) {
            return JSON.parseObject(resultModel.getData(), JumpProtocol.class);
        }
        return super.asyncExecute(url, resultModel);
    }

    @Override
    public void onSucceed(String url, ResultModel resultModel) throws JSONException, RemoteException {
        super.onSucceed(url, resultModel);
        if (url.contains(URL_JUMP_PROTOCOL)) {
            mJumpProtocol = (JumpProtocol) resultModel.getDataModel();
            if (mNotifyDialog != null) {
                if (!StringUtil.isNullorEmpty(mJumpProtocol.getText2())) {
                    mNotifyDialog.show(mJumpProtocol.getTitle(), mJumpProtocol.getCoverUrl(), mJumpProtocol.getContent(), mJumpProtocol.getText2(), mJumpProtocol.getText1());
                } else {
                    mNotifyDialog.showSingle(mJumpProtocol.getTitle(), mJumpProtocol.getCoverUrl(), mJumpProtocol.getContent(), mJumpProtocol.getText1());

                }
            }
        }
    }

}
